﻿// ===============================================================================
// DotNet.Platform 开发框架 2016 版权所有
// ===============================================================================

using System;
using System.Collections.Generic;
using System.Linq;
using DotNet.Core.Entity;
using DotNet.Collections;
using DotNet.Data;
using DotNet.Extensions;
using DotNet.Utility;
using DotNet.Helper;

namespace DotNet.Core.Service
{
    /// <summary>
    /// 意见反馈服务
    /// </summary>
    public class FeedbackService
    {
        private CoreRepository<Feedback> repos = new CoreRepository<Feedback>();

        /// <summary>
        /// 保存
        /// </summary>
        /// <param name="entity">实体</param>
        public BoolMessage Insert(Feedback entity)
        {
            var result = EntityHelper.ValidAsMessage(entity, "<br>");
            if (result.Failure)
            {
                return result;
            }
            entity.Number = DateTime.Now.ToString("yyyyMMddHHmmssffff");
            entity.CreateTime = DateTime.Now;
            repos.Insert(entity);
            return BoolMessage.True;
        }

        /// <summary>
        /// 回复
        /// </summary>
        /// <param name="feedbackId">建议Id</param>
        /// <param name="entity">实体</param>
        public BoolMessage Revert(string feedbackId, Revertback rentity)
        {
            rentity.RevertTime = DateTime.Now;
            var result = EntityHelper.ValidAsMessage(rentity, "<br>");
            if (result.Failure)
            {
                return result;
            }

            var entity = new Feedback();
            entity.RevertDept = rentity.RevertDept;
            entity.RevertTime = rentity.RevertTime;
            entity.RevertContents = rentity.RevertContents;
            entity.Status = rentity.Status;
            repos.UpdateInclude(entity, 
                p => p.Id == feedbackId, 
                p => p.RevertDept, p => p.RevertTime, p => p.RevertContents, p => p.Status);
            return BoolMessage.True;
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="ids">主键数组</param>
        public BoolMessage Delete(string[] ids)
        {
            repos.Delete(ids);
            return BoolMessage.True;
        }

        /// <summary>
        /// 获取单个对象
        /// </summary>
        /// <param name="id">主键</param>
        public Feedback GetById(string id)
        {
            return repos.Get(id);
        }

        /// <summary>
        /// 获取列表
        /// </summary>
        /// <param name="search">数据对象</param>
        public List<Feedback> GetList(FeedbackSearch search)
        {
            var query = repos.SQL;
            SetQuery(query, search);
            return repos.Query(query).ToList();
        }

        /// <summary>
        /// 获取分页列表
        /// </summary>
        /// <param name="pageCondition">分页对象</param>
        /// <param name="search">数据对象</param>
        /// <param name="hasContent">是否包含内容</param>
        public PageList<Feedback> GetPageList(PaginationCondition pageCondition, FeedbackSearch search,bool hasContent = false)
        {
            pageCondition.SetDefaultOrder(nameof(Feedback.CreateTime));
            var query = repos.PageQuery(pageCondition);
            SetQuery(query, search);
            if (!hasContent)
            {
                query.ExcludeSelect(p => p.Contents, p => p.RevertContents);
            }
            return repos.Page(query);
        }

        /// <summary>
        /// 设置查询条件
        /// </summary>
        /// <param name="query">查询对象</param>
        /// <param name="search">数据对象</param>
        public void SetQuery(SQLQuery<Feedback> query, FeedbackSearch search)
        {
            if (search.Number.IsNotEmpty())
            {
                search.Number = search.Number.Trim();
                query.Where(p => p.Number.Contains(search.Number));
            }
            if (search.Title.IsNotEmpty())
            {
                search.Title = search.Title.Trim();
                query.Where(p => p.Title.Contains(search.Title));
            }
            if (search.Tel.IsNotEmpty())
            {
                search.Tel = search.Tel.Trim();
                query.Where(p => p.Tel.Contains(search.Tel));
            }
            if (search.Category.IsNotEmpty())
            {
                search.Category = search.Category.Trim();
                query.Where(p => p.Category == search.Category);
            }
            if (search.Status.HasValue)
            {
                query.Where(p => p.Status == search.Status.Value);
            }
            if (search.CreateStartDate.HasValue)//开始日期
            {
                query.Where(p => p.CreateTime >= search.CreateStartDate.Value);
            }
            if (search.CreateEndDate.HasValue)//结束日期
            {
                query.Where(p => p.CreateTime <= search.CreateEndDate.Value.AddDays(1));
            }
        }

        /// <summary>
        /// 获取分页列表（前端模糊查询）
        /// </summary>
        /// <param name="pageCondition"></param>
        /// <param name="search"></param>
        /// <param name="hasContent"></param>
        /// <returns></returns>
        public PageList<Feedback> GetPageList_FuzzySearch(PaginationCondition pageCondition, FeedbackSearch search, bool hasContent = false)
        {
            pageCondition.SetDefaultOrder(nameof(Feedback.CreateTime));
            var query = repos.PageQuery(pageCondition);
            SetQuery_FuzzySearch(query, search);
            if (!hasContent)
            {
                query.ExcludeSelect(p => p.Contents, p => p.RevertContents);
            }
            return repos.Page(query);
        }
        /// <summary>
        /// 设置查询条件（前端模糊查询用）
        /// </summary>
        /// <param name="query"></param>
        /// <param name="search"></param>
        public void SetQuery_FuzzySearch(SQLQuery<Feedback> query, FeedbackSearch search)
        {

            if (search.Title.IsNotEmpty())
            {
                search.Title = search.Title.Trim();
                search.Number = search.Title;
                search.Contents = search.Title;
                search.RevertContents = search.Title;
                
                query.Where(p => (p.Title.Contains(search.Title) ||  p.Number.Contains(search.Number) || p.Contents.Contains(search.Contents) || p.RevertContents.Contains(search.RevertContents)));
           
            }
            if (search.Status.HasValue)
            {
                query.Where(p => p.Status == search.Status.Value);
            }
            if (search.Category.IsNotEmpty())
            {
                search.Category = search.Category.Trim();
                query.Where(p => p.Category == search.Category);
            }


        }
    }
}
